﻿@{
    ViewBag.Title = "下载文件";
    Layout = "~/Views/Shared/_MobileLayout.cshtml";

    var browser = YYS.v3Helper.whatBrowser();

    if (browser == 1 || browser == 4)
    {
        <style type="text/css">
            body {
                background-color: #000;
            }
        </style>
        <div style="user-select:none; height:100%;width:100%; background-image:url('@(Request.v3JsCss("images/wxdownload.jpg"))'); background-repeat:no-repeat; background-size:contain; background-position:right top;"></div>
        return;
    }

    var obj = Request.QueryString["obj"].xTrim();

    if (string.IsNullOrWhiteSpace(obj) || obj.Length <= 8)
    {
        Html.RenderPartial("Result", new YYS.ResultModel() { IsPart = 1, Icon = 0, Title = "提示", Msg = "请求缺少必须的参数" });
        return;
    }

    string name = null, size = null, url = null;

    try
    {
        var uni = YYS.v3Helper.uniDecrypt(obj);
        if (uni.HasError)
        {
            Html.RenderPartial("Result", new YYS.ResultModel() { IsPart = 1, Icon = 0, Msg = uni.Error });
            return;
        }
        string msg = uni.Obj;

        try
        {
            var json = msg.xConvertToJObject();
            var data = json["data"];
            name = data.Value<string>("name").xTrim();
            size = data.Value<string>("size").xTrim();
            url = data.Value<string>("url").xTrim();
            var exp = data.Value<long>("exp");

            if (exp > 0 && exp < DateTime.Now.xToTimeStamp())
            {
                Html.RenderPartial("Result", new YYS.ResultModel() { IsPart = 1, Icon = 0, Title = "提示", Msg = "下载地址已失效[" + exp + "]" });
                return;
            }

            if (!url.ToLower().StartsWith("http://") && !url.ToLower().StartsWith("https://"))
            {
                Html.RenderPartial("Result", new YYS.ResultModel() { IsPart = 1, Icon = 0, Title = "提示", Msg = "参数格式不正确.URL" });
                return;
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                var arr = url.Split('/');
                name = arr[arr.Length - 1];
            }
        }
        catch (Exception ex)
        {
            Html.RenderPartial("Result", new YYS.ResultModel() { IsPart = 1, Icon = 0, Title = "提示", Msg = ex.Message });
            return;
        }
    }
    catch (Exception ex)
    {
        Html.RenderPartial("Result", new YYS.ResultModel() { IsPart = 1, Icon = 0, Title = "提示", Msg =  ex.Message });
        return;
    }
    <script type="text/javascript">function download() {window.$jscript.fn.downloadFile('@(url.xToBase64())', '@(name)', true);}</script>
    Html.RenderPartial("Result", new YYS.ResultModel() { IsPart = 1, Icon = 2, Title = "下载文件", Msg = name, OkButton = "立即下载 " + size, OkLink = "javascript:download();" });
}

